home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / umddvi / lib / scaletfm.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  3KB  |  112 lines

  1. /*
  2.  * Copyright (c) 1987 University of Maryland Department of Computer Science.
  3.  * All rights reserved.  Permission to copy for any purpose is hereby granted
  4.  * so long as this copyright notice remains intact.
  5.  */
  6.  
  7. #ifndef lint
  8. static char rcsid[] = "$Header: scaletfm.c,v 2.5 87/06/16 18:28:49 chris Exp $";
  9. #endif
  10.  
  11. #include "types.h"
  12. #include "font.h"
  13.  
  14. /*
  15.  * From DVITYPE.WEB:
  16.  *
  17.  * ``The most important part of in_TFM is the width computation, which
  18.  *   involvles multiplying the relative widths in the TFM file by the scaling
  19.  *   factor in the DVI file.  This fixed-point multiplication must be done with
  20.  *   precisely the same accuracy by all DVI-reading programs, in order to
  21.  *   validate the assumptions made by DVI-writing programs like \TeX 82.
  22.  *
  23.  *   Let us therefore summarize what needs to be done.  Each width in a TFM
  24.  *   file appears as a four-byte quantity called a fix_word.  A fix_word whose
  25.  *   respective bytes are (a,b,c,d) represents the number
  26.  *
  27.  *       {{ b * 2^{-4} + c * 2^{-12} + d * 2^{-20},        if a = 0;
  28.  *    x = {{
  29.  *       {{ -16 + b * 2^{-4} + c * 2^{-12} + d * 2^{-20},  if a = 255.
  30.  *
  31.  *   (No other choices of a are allowed, since the magnitude of a TFM dimension
  32.  *   must be less than 16.)  We want to multiply this quantity by the integer
  33.  *   z, which is known to be less than 2^{27}.  Let \alpha = 16z.  If z <
  34.  *   2^{23}, the individual multiplications b * z, c * z, d * z cannot
  35.  *   overflow; otherwise we will divide z by 2, 4, 8, or 16, to obtain a
  36.  *   multiplier less than 2^{23}, and we can compensate for this later.  If z
  37.  *   has thereby been replaced by z' = z/2^e, let \beta = 2^{4-e}; we shall
  38.  *   compute
  39.  *
  40.  *    \lfloor (b + c * 2^{-8} + d * 2^{-16})z' / \beta \rfloor
  41.  *
  42.  *   if a = 0, or the same quantity minus \alpha if a = 255.  This calculation
  43.  *   must be done exactly, for the reasons stated above; the following program
  44.  *   does the job in a system-independent way, assuming that arithmetic is
  45.  *   exact on numbers less than 2^{31} in magnitude.''
  46.  */
  47.  
  48. /*
  49.  * Scale the single TFM width t by z.
  50.  */
  51. i32
  52. ScaleOneWidth(t, z)
  53.     register i32 t, z;
  54. {
  55.     register i32 alpha, log2beta;
  56.  
  57.     /* First compute \alpha, \beta, and z': */
  58.     alpha = 16 * z;
  59.     log2beta = 4;
  60.     while (z >= (1 << 23)) {
  61.         z >>= 1;
  62.         log2beta--;
  63.     }
  64.  
  65.     /* The four values 'a', 'b', 'c', and 'd' are fields within t: */
  66. #define    a    (UnSign8(t >> 24))
  67. #define    b    (UnSign8(t >> 16))
  68. #define    c    (UnSign8(t >> 8))
  69. #define    d    (UnSign8(t))
  70.     if (t) {
  71.         t = (((((d * z) >> 8) + c * z) >> 8) + b * z) >> log2beta;
  72.         if (a) {
  73.             if (a != 255)
  74.                 error(0, 0, "bad TFM width! [ScaleOneWidth]");
  75.             t -= alpha;
  76.         }
  77.     }
  78.     return (t);
  79. }
  80.  
  81. /*
  82.  * Scale a set of glyphs [l..h) in font f according to f->f_dvimag.
  83.  */
  84. ScaleGlyphs(f, l, h)
  85.     register struct font *f;
  86.     int l, h;
  87. {
  88.     register int i;
  89.     register i32 t, z, alpha, log2beta;
  90.  
  91.     z = f->f_dvimag;
  92.     alpha = 16 * z;
  93.     log2beta = 4;
  94.     while (z >= (1 << 23)) {
  95.         z >>= 1;
  96.         log2beta--;
  97.     }
  98.  
  99.     for (i = l; i < h; i++) {
  100.         if ((t = f->f_gly[i]->g_tfmwidth) == 0)
  101.             continue;
  102.         t = (((((d * z) >> 8) + c * z) >> 8) + b * z) >> log2beta;
  103.         if (a) {
  104.             if (a != 255)
  105.                 error(0, 0, "\"%s\", glyph %d: bad TFM width",
  106.                     f->f_path, i);
  107.             t -= alpha;
  108.         }
  109.         f->f_gly[i]->g_tfmwidth = t;
  110.     }
  111. }
  112.